package com.wzh.service;

import com.wzh.dao.CardDao;
import com.wzh.dao.UserDao;


import com.wzh.pojo.Po.*;
import com.wzh.pojo.Req.DataReq;
import com.wzh.pojo.Resp.DataResp;
import com.wzh.util.CommmonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
@Service
public class Userser {
	@Autowired
	private UserDao userdao;
	@Autowired
	private CardDao cardDao;
	public List<User> get(){
		return userdao.getAll();
	};

	public boolean delete(User user){
		return userdao.deonebyid(user)>0;
	}

	public User login(User user) {
		return userdao.login(user);
	}

	public Boolean addMoney(User user) {
		int res=userdao.Sendrecoed(user);
		return userdao.addmoney(user)>0;
	}

	public boolean register(User user2) {
		boolean res=false;
		if (userdao.findUserid(user2)==null){
			if (userdao.register(user2)>0){
				res=true;
			}
		}
		return res;
	}



	public List<NameList> readNameList() {

		return userdao.readList();
	}

	public DataResp<Object> show(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		List<User> list= userdao.findall(req);
		if (list.size()!=0){
			resp.setData(list);
			resp.setMes("查询成功！");
		}else{
			resp.setMes("查询失败！");
		}
		return  resp;
	}
    @Transactional
	public boolean delCoin(User user2) {

		User user =userdao.findUserid(user2);
		if(user.getMoney()<user2.getMoney()){
			return false;
		}

		return userdao.delmoney(user2)>0;
	}

	public DataResp<Object> sendCashRecord(Cash cash) {
		DataResp<Object> resp=new DataResp<>();
		cash.setCashId(CommmonUtil.getUUID());
		cash.setTime(CommmonUtil.getNowTime());
		//判断是否小于30
		if(cash.getMoney()<30){
			resp.setMes("余额小于30不可提现！");
			return resp;
		}

		Cash  record= userdao.findCashRecord(cash);
		if (record==null){
			int res=userdao.sendCashRecord(cash);
			if (res>0){
				resp.setMes("提交成功！请耐心等待");
			}else{
				resp.setMes("提交失败！");
			}
		}else{
			resp.setMes("你有一条提现申请正在审核...请耐心等待");
		}
		return  resp;
	}
@Transactional
	public DataResp<Object> checkCashRecord(Cash cash) {
		DataResp<Object> resp=new DataResp<>();
		Integer res = userdao.checkCashRecord(cash);
		if (res!=0){
			//修改状态
			User user =new User();
			user.setMoney(cash.getMoney());
			user.setUserid(cash.getIphone());
			boolean delCoinRes=false;
			//打款才扣余额
			if(cash.getStatus()==1){
				// 1同意打款 2拒绝
				//查看是否分佣
				User isShareMoney =userdao.findUserid(user);
				if (isShareMoney.getRecommend()!=null){
					User shareUser =new User();
					shareUser.setMoney(cash.getMoney()/10);
					shareUser.setUserid(isShareMoney.getRecommend());
					userdao.addmoney(shareUser);
				}
				delCoinRes = delCoin(user);

			}
			if (delCoinRes){
				resp.setMes("操作成功！");
			}else {
				resp.setMes("操作失败！");
			}
		}else{
			resp.setMes("操作失败！");
		}

		return  resp;
	}

	public DataResp getCashRecord(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		List<Cash> list= userdao.getCashRecord(req);
		if (list.size()!=0){
			resp.setData(list);
			resp.setMes("查询成功！");
		}else{
			resp.setMes("查询失败！");
		}
		return  resp;
	}


	public DataResp<Object> getMoneyRecord(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		List<Cash> list= userdao.getMoneyRecord(req);
		if (list.size()!=0){
			resp.setData(list);
			resp.setMes("查询成功！");
		}else{
			resp.setMes("查询失败！");
		}
		return  resp;
	}

	public boolean boundDev(User user) {
		return userdao.boundDev(user)>0;
	}

	public boolean DelDev(User user) {
		return userdao.DelDev(user)>0;
	}

	public boolean UpdateVipTime(User user) {
		return userdao.UpdateVipTime(user)>0;
	}

	public DataResp<Object> addRecommend(User user) {

		DataResp<Object> resp=new DataResp<>();
		//查询是否已经绑定过推荐人
		User my =userdao.findUserid(user);
		if(my.getRecommend()!=null){
			resp.setMes("你已经绑定过推荐人了,不可操作");
			return resp;
		}
		//查询推荐人是否存在
		User selectUser=new User();
		selectUser.setUserid(user.getRecommend());
		User reUser =userdao.findUserid(selectUser);
		if (reUser!=null){
		int res=userdao.addRecommend(user);
		if (res<=0){
			resp.setMes("操作失败,请重试！");
		}
		}else{
			resp.setMes("不存在该推荐人");
		}
		return  resp;
	}

	public DataResp<Object> ShowMyCustomer(User user) {
		DataResp<Object> resp=new DataResp<>();
		List<User> users=userdao.ShowMyCustomer(user);
		resp.setPageCount(users.size());
		resp.setData(users);
		return resp;
	}


	/**
	 * 上传刷机记录 根据时间
	 * @param user [com.wzh.pojo.Po.User]
	 * @return boolean
	 * @since 2023/6/10 10:17
	 * @author WZH
	 */

	public boolean addUserRecord(User user) {
		Calendar calendar = Calendar.getInstance(); //
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");


		UserRecord userRecord =userdao.selectNowUserRecord(user,formatter.format(calendar.getTime()));
		if (userRecord==null){
			HashMap<String,Object> map = new HashMap<>();
			map.put("userid", user.getUserid());
			map.put("time",formatter.format(calendar.getTime()));
			int res=userdao.sendNowUserRecord(map);
		}else{
			int res2=userdao.UpdateNowUserRecord(user,formatter.format(calendar.getTime()));
		}

		return true;
	}

	public DataResp<Object> addXcxName(DataReq req) {
		DataResp<Object> resp=new DataResp<>();
		int res= userdao.addXcxName(req);
		if (res!=0){
			resp.setMes("操作成功！");
		}else{
			resp.setMes("操作失败！");
		}
		return  resp;
	}
}
